package com.yunhe.rentcars.pro.controller.permi;



import com.yunhe.rentcars.commons.dto.JSONResponse;
import com.yunhe.rentcars.commons.dto.Pageable;
import com.yunhe.rentcars.pro.entity.permi.Role;
import com.yunhe.rentcars.pro.entity.permi.User;
import com.yunhe.rentcars.pro.service.permi.RoleService;
import com.yunhe.rentcars.pro.service.permi.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017/10/12 0012.
 */
@Controller
@RequestMapping("/user")
public class UserController{

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService ;

    /**
     * 加载用户列表页
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping
    public String list(User user,HttpServletRequest req){
        req.setAttribute("user",user);
        return "permi/user/list";
    }

    /**
     * 加载用户添加页
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/add")
    public ModelAndView add(){

        ModelAndView mav = new ModelAndView("permi/user/add");
        List<Role> roleList = roleService.findListCanUse();
        mav.addObject("roleList",roleList);

        return mav;
    }

    /**
     * 加载用户编辑页
     * @param req
     */
    @RequestMapping("/edit")
    public ModelAndView edit(HttpServletRequest req, Long id){
        User user1 = new User();
        user1.setUid(id);
        User user = userService.find(user1);
        List<Role> roleList = roleService.findListCanUse();

        ModelAndView mav = new ModelAndView("permi/user/edit");
        mav.addObject("user",user);
        mav.addObject("roleList",roleList);
        return mav;

    }

    /**
     * 删除用户方法
     */
    @RequestMapping("/delete")
    public String del(Long id){
        userService.delete(id);
        return "redirect:/user";
    }

    /**
     * 批量删除用户方法
     * @throws IOException
     */
    @RequestMapping("/delBatch")
    @ResponseBody
    public JSONResponse delBatch(String ids){
        userService.deleteBatch(ids);
        return JSONResponse.success("删除成功");
    }

    /**
     * 添加用户方法
     * @throws IOException
     */

    @RequestMapping("/save")
    public String save(User user){
        if(null != user){
            userService.save(user);
        }
        return "redirect:/user";
    }


    /**
     * 修改用户信息
     * @param user
     * @return
     * @throws SQLException
     */
    @RequestMapping("/update")
    public String update(User user){
        if(null != user){
            userService.update(user);
        }
        return "redirect:/user";
    }


    @RequestMapping("/listData")
    @ResponseBody
    public Pageable listData(Integer offset, Integer limit,User user){
        return userService.findPage(offset, limit,user);
    }


    @RequestMapping("/onlyOne")
    @ResponseBody
    public JSONResponse onlyOne(String userName){
        User user = new User();
        user.setUsername(userName);
        Boolean flag = userService.findOne(user);
        return JSONResponse.success("校验成功",flag);

    }

    @RequestMapping("/onlyOne2")
    @ResponseBody
    public String onlyOne2(String userName){
        User user = new User();
        user.setUsername(userName);
        Boolean flag = userService.findOne(user);
        return flag.toString();
    }


    @RequestMapping("/sendEmail")
    @ResponseBody
    public JSONResponse sendEmail(User user){
        String msg = userService.sendEmail(user);
        return JSONResponse.success(msg);
    }


    @RequestMapping("/resetPass")
    @ResponseBody
    public JSONResponse mmpd(HttpServletRequest req, String pwd, String newPwd, String newPwd2){
        User user = (User) req.getSession().getAttribute("user");
        String message = "";
        if(pwd.equals(user.getPassword())){
            if(newPwd.equals(newPwd2)){
                if(!newPwd.equals(user.getPassword())){

                    User user1 = new User();
                    user1.setPassword(newPwd);
                    user1.setUid(user.getUid());
                    userService.update(user);
                    return JSONResponse.success("密码修改成功");
                }else{
                    message = "新密码与原密码相同";
                }
            }else{
                message = "两次密码不一致";
            }
        }else{
            message = "用户密码输入错误";
        }
        return JSONResponse.fail("",message);

    }

}
